<template>
  <div>
    <TopFilter ref="topFilter" :list="topFilterList" :cols="3" labelWidth="90" @filterChange="changeHandle" @onCollapse="collapseHandle"></TopFilter>

    <FilterTable
      ref="table"
      :columns="columns"
      :fetchapi="fetchApi"
      :params="fetchParams"
      :datakey="'records'"
      :height="'auto'"
      :isToperInfo="false"
      :onRowSelectChange="selectChange"
      columnsRef="ysjBirthday2019100801Table"
      :onColumnsChange="columns => (this.columns = columns)"
    >
      <template slot="controls">
        <el-button :disabled="isChoice" size="small" type="primary" icon="el-icon-plus" @click="care">关爱</el-button>
        <el-button size="small" type="primary" icon="el-icon-plus" @click="careAll">全部关爱</el-button>
      </template>
    </FilterTable>

    <Drawer
      :visible.sync="visible"
      :title="type == 'add' ? '关爱' : '关爱明细'"
      :width="1200"
      destroyOnClose
      :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }"
    >
      <customerBirthdayDetail :type="type" :selectedRows="selectedRows" @changeHandle="changeHandle" @close="closeHandler" />
    </Drawer>
  </div>
</template>
<script>
import { dictionary } from '@/utils/dictMixin';
import { formatdate } from '@/utils/index';
import { getCareObjList } from '@/api/customerRepair/customerCare/potentialCustomerBirthday2';
import customerBirthdayDetail from './customerBirthdayDetail';

export default {
  name: 'customerBirthday',
  components: { customerBirthdayDetail },
  mixins: [dictionary],
  data() {
    return {
      columns: this.createTableColumns(),
      topFilterList: this.createTopFilters(),
      fetchApi: getCareObjList,
      fetchParams: { careStatusList: '15921001,15921002' },
      selectedRows: [],
      isChoice: true,
      type: '',
      visible: false
    };
  },
  methods: {
    createTopFilters() {
      return [
        {
          type: 'RANGE_DATE',
          label: '客户生日',
          fieldName: 'birthDayStart|birthDayEnd',
          placeholder: '客户生日'
        },
        {
          type: 'INPUT',
          label: '客户编号',
          fieldName: 'customerNo',
          placeholder: '请输入...'
        },
        {
          type: 'INPUT',
          label: '客户名称',
          fieldName: 'customerName',
          placeholder: '请输入...'
        },
        {
          type: 'RANGE_DATE',
          label: '关爱日期',
          fieldName: 'careDateStart|careDateEnd',
          placeholder: '关爱日期'
        },
        {
          type: 'MULTIPLE_SELECT',
          label: '关爱状态',
          fieldName: 'careStatusList',
          placeholder: '请选择...',
          itemList: this.createDictList('1592'),
          initialValue: [15921001, 15921002]
        }
      ];
    },
    createTableColumns() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          align: 'center',
          width: 70,
          fixed: 'left',
          sorter: true,
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        {
          title: '客户编号',
          dataIndex: 'customerNo',
          sorter: true,
          filter: true,
          width: 125,
          filterType: 'input'
        },
        {
          title: '客户名称',
          dataIndex: 'customerName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '手机号',
          dataIndex: 'mobilePhone',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '销售顾问',
          dataIndex: 'consultant',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '建档日期',
          dataIndex: 'createdAt',
          sorter: true,
          filter: true,
          filterType: 'input',
          render: props => {
            if (props.row.createdAt && props.row.createdAt != '') {
              return <div>{formatdate(new Date(parseInt(props.row.createdAt)), 1)}</div>;
            }
          }
        },
        {
          title: '性别',
          dataIndex: 'gender',
          sorter: true,
          filter: true,
          filterType: 'input',
          render: props => {
            return <div>{this.createDictText(props.row.gender, '1002')}</div>;
          }
        },
        {
          title: '出生日期',
          dataIndex: 'birthday',
          sorter: true,
          filter: true,
          filterType: 'input',
          render: props => {
            if (props.row.birthday && props.row.birthday != '') {
              return <div>{formatdate(new Date(parseInt(props.row.birthday)), 1)}</div>;
            }
          }
        },
        {
          title: '任务编号',
          dataIndex: 'taskNo',
          sorter: true,
          filter: true,
          width: 125,
          filterType: 'input'
        },
        {
          title: '关爱名称',
          dataIndex: 'careName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '要求完成日期',
          dataIndex: 'requiredDate',
          sorter: true,
          filter: true,
          filterType: 'input',
          render: props => {
            if (props.row.requiredDate && props.row.requiredDate != '') {
              return <div>{formatdate(new Date(parseInt(props.row.requiredDate)), 1)}</div>;
            }
          }
        },
        {
          title: '最后关爱日期',
          dataIndex: 'careDate',
          sorter: true,
          filter: true,
          filterType: 'input',
          render: props => {
            if (props.row.careDate && props.row.careDate != '') {
              return <div>{formatdate(new Date(parseInt(props.row.careDate)), 1)}</div>;
            }
          }
        },
        {
          title: '下次关爱日期',
          dataIndex: 'nextCareDate',
          sorter: true,
          filter: true,
          filterType: 'input',
          render: props => {
            if (props.row.nextCareDate && props.row.nextCareDate != '') {
              return <div>{formatdate(new Date(parseInt(props.row.nextCareDate)), 1)}</div>;
            }
          }
        },
        {
          title: '关爱状态',
          dataIndex: 'careStatus',
          sorter: true,
          filter: true,
          filterType: 'input',
          render: props => {
            return <div>{this.createDictText(props.row.careStatus, '1592')}</div>;
          }
        },
        {
          title: '关爱内容',
          dataIndex: 'careContent',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '关爱失败原因',
          dataIndex: 'careFailureReason	',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '客户反馈',
          dataIndex: 'customerFeedback',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '客户类型',
          dataIndex: 'customerType',
          sorter: true,
          filter: true,
          filterType: 'input',
          render: props => {
            return <div>{this.createDictText(props.row.customerType, '1523')}</div>;
          }
        },
        {
          title: '意向级别',
          dataIndex: 'intentLevel',
          sorter: true,
          filter: true,
          filterType: 'input',
          render: props => {
            return <div>{this.createDictText(props.row.intentLevel, '1519')}</div>;
          }
        }
      ];
    },
    changeHandle(val) {
      this.fetchParams = Object.assign({}, val);
    },
    collapseHandle() {
      this.$nextTick(() => {
        this.BaseTable.EXECUTE_RESET_HEIGHT();
      });
    },
    closeHandler() {
      this.visible = false;
    },
    selectChange(rows) {
      // 选中时触发的函数
      this.selectedRows = rows;
      this.isChoice = this.selectedRows.length > 0 ? false : true;
    },
    care() {
      this.type = 'edit';
      this.visible = true;
    },
    careAll() {
      this.type = 'edit';
      this.visible = true;
    },
    submitCare() {
      let param = [];
      this.selectedRows.map(obj => {
        let data = { carePlanId: obj.carePlanId, recordVersion: obj.recordVersion };
        param.push(data);
      });
    }
  }
};
</script>
